package com.fs.ibox_h.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fs.ibox_h.pojo.Botice;
import com.fs.ibox_h.servie.BoticeService;
import com.fs.ibox_h.util.BeanFactory;
import com.fs.ibox_h.util.ResponseDataUtil;
import com.fs.ibox_h.util.ResponseJSONUtil;
import com.fs.ibox_h.vo.Page;
import com.fs.ibox_h.vo.ResponseData;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Date;

@WebServlet(name = "BoticeServlet", value = "/BoticeServlet/*")
public class BoticeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        //获取前端传递的json数据
//        StringBuffer stringBuffer = new StringBuffer();
//        //请求输入流
//        BufferedReader reader = request.getReader();
//        String line = null;
//
//        while((line = reader.readLine()) != null){
//            stringBuffer.append(line);
//        }
//        //获取json中的数据
//        JSONObject js = JSON.parseObject(stringBuffer.toString());
        String path = request.getPathInfo();
        if ("/add".equals(path)){
            add(request,response);
        }
        else if("/queryAll".equals(path)){
            queryAll(request,response);
        }
        else if("/del".equals(path)){
            del(request,response);
        }
    }

    private void del(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        int intId=Integer.parseInt(id);
        BoticeService bean = BeanFactory.createBean(BoticeService.class);
        bean.delByid(intId);
        ResponseData mmsg = ResponseDataUtil.buildOk("删除成功");
        ResponseJSONUtil.responseJSON(mmsg,response);

    }

    private void queryAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        String currentPage = js.getString("currentPage");
//        String pageSize = js.getString("pageSize");
        String currentPage = request.getParameter("currentPage");
        String pageSize = request.getParameter("pageSize");
        int intcurrentPage=Integer.parseInt(currentPage);
        int intpageSize=Integer.parseInt(pageSize);
        BoticeService bean = BeanFactory.createBean(BoticeService.class);
        Page<Botice> boticePage = bean.queryAll(intcurrentPage, intpageSize);
        ResponseData responseData = ResponseDataUtil.buildOk(boticePage);
        ResponseJSONUtil.responseJSON(responseData,response);

    }

    private void add(HttpServletRequest request, HttpServletResponse response ) throws IOException {
        Botice botice = new Botice();
//        String tname = js.getString("name");
//        String region = js.getString("region");
//        String date1 = js.getString("date1");
//        String type = js.getString("type");
        String tname = request.getParameter("name");
        String region = request.getParameter("region");
        String date1 = request.getParameter("date1");
        String type = request.getParameter("type");
        botice.setTitle(tname);
        botice.setContent(region);
        int inttype=Integer.parseInt(type);
        botice.setB_type(inttype);
        long ldate=Long.parseLong(date1);
        Date date=new Date(ldate);
        botice.setB_time(date);
        BoticeService bean = BeanFactory.createBean(BoticeService.class);
        bean.add(botice);
        ResponseData mmsg = ResponseDataUtil.buildOk("成功");
        ResponseJSONUtil.responseJSON(mmsg,response);
    }
}
